/**
 * Created by lmn98 on 2017/5/19.
 */
var Sequelize = require('sequelize');
var conf = require('../config');
var sequelize = new Sequelize(conf.mysql.database, conf.mysql.uname, conf.mysql.passwd, {
    host: conf.mysql.host,
    port: conf.mysql.port,
    dialect: 'mysql',
    pool: {
        maxConnections: 200,
        minConnections: 20,
        maxIdleTime: 2000
    },
    timezone: '+08:00',
    logging: false,
    maxConcurrentQueries: 100
});

var user = sequelize.define('user',{
    username: Sequelize.STRING(30),
    password: Sequelize.STRING
});

var torrent = sequelize.define('torrent',{
    infoHash: {
        type: Sequelize.STRING,
        primaryKey: true
    },
    name: Sequelize.STRING,
    dir: Sequelize.STRING,
    path: Sequelize.STRING,
    size: Sequelize.STRING(40),
    state: Sequelize.INTEGER(1)
});
torrent.belongsTo(user);
user.hasMany(torrent);
var torrent_file = sequelize.define('torrent_file',{
    path: Sequelize.STRING,
    name: Sequelize.STRING,
    length: Sequelize.INTEGER,
    offset: Sequelize.INTEGER
},{
    timestamps: false
});
torrent_file.belongsTo(torrent);
torrent.hasMany(torrent_file);

var transcode = sequelize.define('transcode', {
    workNo: {
        type: Sequelize.STRING(20),
        primaryKey: true
    },
    name: Sequelize.STRING,
    dir: Sequelize.STRING,
    command: Sequelize.INTEGER(1),
    state: Sequelize.INTEGER(1)
},{
    indexes: [{
        fields: ['workNo']
    }]
});
transcode.belongsTo(user);
user.hasMany(transcode);


sequelize.sync();

module.exports = {
    user: user,
    torrent: torrent,
    torrent_file: torrent_file,
    transcode: transcode
};